package com.twu.biblioteca;

import java.util.HashMap;


/**
 * Created with IntelliJ IDEA.
 * User: Thoughtworker
 * Date: 7/11/13
 * Time: 10:33 AM
 * To change this template use File | Settings | File Templates.
 */
public class UserDataBase {
    private HashMap<String, User> users;
    private String loggedInUserName;

    public UserDataBase(String username, User userProfile) {
        users= new HashMap<String, User>();
        users.put(username,userProfile);
        loggedInUserName = "";
    }

    public boolean validateUser(String user) {
        return users.containsKey(user);
    }

    public boolean validatePassword(String userName, String password) {
            return password.equals(users.get(userName).getPassword());
    }

    public boolean checkUserCredentials() {
        System.out.print("Username: ");
        String userName = Menu.getUserInput();
        if(this.validateUser(userName)){
            System.out.print("Password: ");
            if(this.validatePassword(userName, Menu.getUserInput())){
                users.get(userName).setLoggedIn(true);
                loggedInUserName = userName;
                return true;
            }
            else{
                System.out.println("Wrong password!");
                return false;
            }
        }
        else{
            System.out.println("User not found!");
            return false;
        }
    }

    public boolean isUserLoggedIn() {
        return !loggedInUserName.isEmpty();
    }

    public void printLoggedInUser() {
        System.out.println("\nName: " + users.get(loggedInUserName).getName());
        System.out.println("Email: " + users.get(loggedInUserName).getEmail());
        System.out.println("Phone #: " + users.get(loggedInUserName).getPhone()+'\n');

    }
}
